package com.sda.huoshui.base

import android.content.Intent
import android.content.res.Configuration
import android.content.res.Resources
import android.os.Build
import android.os.Bundle
import android.view.View
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity
import com.blankj.utilcode.util.AdaptScreenUtils
import com.blankj.utilcode.util.KeyboardUtils
import com.sda.huoshui.R
import com.sda.huoshui.api.RetrofitHelper
import com.sda.huoshui.api.RetrofitService
import com.sda.huoshui.dialog.WaitDialog
import com.wzd.taijiapp.util.ActivityContainer

import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import rx.subscriptions.CompositeSubscription


abstract class BaseActivity : AppCompatActivity() {

    var retrofitService: RetrofitService? = null
    var mCompositeSubscription: CompositeSubscription? = null
    private var lastClickTime: Long = 0
    private var waitDialog: WaitDialog? = null
    var pageNo = 1
    protected var isFront = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_base)
        if (!ActivityContainer.instance.haveThisActivity(this)) {
            ActivityContainer.instance.addActivity(this)
        }

        setStatusBar(
            resources.getColor(R.color.color_touming),
            isXuanFu = true,
            isBlackColor = true
        )
        retrofitService = RetrofitHelper.getInstance(this).server
        mCompositeSubscription = CompositeSubscription()

        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this)
        }

        //开启滑动返回
//        SlideBack.create().attachToActivity(this)
    }


    protected fun setStatusBar(color: Int, isXuanFu: Boolean, isBlackColor: Boolean) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0及以上
            val decorView = window.decorView

            //对状态栏单独设置颜色
            window.statusBarColor = color

            var option: Int
            if (isXuanFu) {
                option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN//悬浮于布局之上
                if (isBlackColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    option =
                        View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR//悬浮于布局之上
                }
            } else {
                option = View.SYSTEM_UI_FLAG_LAYOUT_STABLE//位于布局之上
                if (isBlackColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    option =
                        View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR//位于布局之上
                }
            }
            decorView.systemUiVisibility = option


        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4到5.0
            val localLayoutParams = window.attributes
            localLayoutParams.flags =
                WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS or localLayoutParams.flags
        }
    }

    /**
     * 是否快速点击了
     */
    protected fun fastClick(): Boolean {
        if (System.currentTimeMillis() - lastClickTime <= 500) {
            return true
        }
        lastClickTime = System.currentTimeMillis()
        return false
    }

    override fun onDestroy() {
        //取消订阅防止内存泄露
        super.onDestroy()
        if (ActivityContainer.instance.haveThisActivity(this)) {
            ActivityContainer.instance.removeActivity(this)
        }
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this)
        }
        if (mCompositeSubscription != null && mCompositeSubscription!!.hasSubscriptions()) {
            mCompositeSubscription!!.unsubscribe()
        }
        if (KeyboardUtils.isSoftInputVisible(this)) {
            KeyboardUtils.hideSoftInput(this)
        }
    }

    /**
     *  跳转页面 isFinsh：是否关闭当前页面
     */
    protected fun goToActivity(intent: Intent, isFinsh: Boolean) {
        startActivity(intent)
        if (isFinsh) {
            finish()
        }
    }


    override fun onResume() {
        super.onResume()
        isFront = true
    }

    override fun onPause() {
        super.onPause()
        isFront = false
    }


    fun showProgressDialog() {
//        if (waitDialog == null) {
//            waitDialog = WaitDialog(this)
//            waitDialog!!.setCancelable(false)
//        }
//        if (!waitDialog!!.isShowing && isFront) {
//            waitDialog!!.show()
//        }
    }

    /**
     * 取消加载dialog
     */
    fun cancelProgressDialog() {
//        if (waitDialog != null && waitDialog!!.isShowing) {
//            waitDialog!!.dismiss()
//        }
    }

    override fun getResources(): Resources {
        val resources: Resources = super.getResources()
        val config = Configuration()
        config.setToDefaults()
        resources.updateConfiguration(config, resources.displayMetrics)
        return AdaptScreenUtils.adaptWidth(resources,360)
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun onUnLoginMessage(message: String) {
    }
}